/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  LogCleanConfigReadServiceImpl.java                                      
 *  File Contents Outline : 日志清洗设置
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package cn.edu.thu.log.web.service.impl;

import cn.edu.thu.log.tree.FromXMLToTree;
import cn.edu.thu.log.tree.ManyTree;
import cn.edu.thu.log.tree.ManyTreeNode;
import cn.edu.thu.log.web.service.LogCleanConfigReadService;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Element;
import org.jdom.JDOMException;

public class LogCleanConfigReadServiceImpl implements LogCleanConfigReadService {

    private static LogCleanConfigReadService singleton = null;
    private String min;
    private String max;
    private ManyTree manyTree;
    private String filename;
    private String classifyTag;
    private int noiseflag=0;
    
    public synchronized static LogCleanConfigReadService instance() {
        if (singleton == null) {
            singleton = new LogCleanConfigReadServiceImpl();
        }
        return singleton;
    }

    @Override
    public void readCleanConfig() {
        //获取日志清洗xml中的根节点     

        try {
            //创建ManyTree树
            FromXMLToTree xmlTree = new FromXMLToTree();
            //读取到xml中表示树的根节点
            Element treexmlroot = xmlTree.readXML(filename);
            //生成多叉树
            ManyTreeNode treeroot = xmlTree.createTree(treexmlroot);
            manyTree = new ManyTree(treeroot);
//            if(manyTree.getRoot()==null){
//            System.out.println("null root");
//            }
//          System.out.printl("genjiedian:"+treeroot.getData().getKey());
//            System.out.println("树的根节点：" + manyTree.getRoot().getData().getNodename());
//            System.out.println("中序遍历结果：" + manyTree.iteratorTree(treeroot));
            //获得最小时间间隔和最大持续时间

            min = xmlTree.getMinValue();
            max = xmlTree.getMaxValue();

        } catch (JDOMException ex) {
            Logger.getLogger(LogCleanConfigReadServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void readNoiseConfig() {
//        System.out.println("read noise config");
          try {
            //创建ManyTree树
            FromXMLToTree xmlTree = new FromXMLToTree();
            System.out.println("haha:filename="+filename);
            //读取到xml中表示树的根节点
            Element treexmlroot = xmlTree.readXML(filename);
 //           System.out.println("noiseFileName in Service:"+filename);
            //生成多叉树
            ManyTreeNode treeroot = xmlTree.createTree(treexmlroot);
            manyTree = new ManyTree(treeroot);
//            if(manyTree.getRoot()==null){
//            System.out.println("null root");
//            }
//          System.out.printl("genjiedian:"+treeroot.getData().getKey());
//            System.out.println("树的根节点：" + manyTree.getRoot().getData().getNodename());
//            System.out.println("中序遍历结果：" + manyTree.iteratorTree(treeroot));
            //获得最小时间间隔和最大持续时间

            min = xmlTree.getMinValue();
            max = xmlTree.getMaxValue();

        } catch (JDOMException ex) {
            Logger.getLogger(LogCleanConfigReadServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String getMin() {
        return min;
    }

    @Override
    public String getMax() {
        return max;
    }

    /**
     * @return the manytree
     */
    @Override
	public ManyTree getManyTree() {
        return manyTree;
    }

    /**
     * @param manytree the manytree to set
     */
    public void setManyTree(ManyTree manyTree) {
        this.manyTree = manyTree;
    }

    @Override
    public void setNoiseFileName(String filename) {
        this.filename=filename;
    }

    /**
     * @return the noiseflag
     */
    @Override
	public int getNoiseflag() {
        return noiseflag;
    }

    /**
     * @param noiseflag the noiseflag to set
     */
    @Override
	public void setNoiseflag(int noiseflag) {
        this.noiseflag = noiseflag;
    }

    @Override
    public void setClassifyTags(String classifyTag) {
       this.classifyTag=classifyTag;
            
    }

    @Override
    public String getClassifyTags() {
        return classifyTag;
    }
}